package poc.utils;

import org.docx4j.TextUtils;
import org.docx4j.XmlUtils;
import org.docx4j.toc.TocHelper;
import org.docx4j.wml.R;
import org.docx4j.wml.Text;
import poc.myrun.MyRun;
import poc.myrun.MyRun_Type1;
import poc.myrun.MyRun_Type2;
import poc.myrun.MyRun_Type3;

import javax.xml.bind.JAXBElement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangleilei on 2018/10/11.
 */
public class RunUtil {
    public static List<R> getRuns(Object p, String xmlName){
        List<R> result=new ArrayList<R>();
        List<Object> runs = TocHelper.getAllElementsFromObject(p, R.class);
        for (Object r:runs){
            R run=(R)r;
            List<Object> runChilds= run.getContent();
            for (Object runChild :runChilds){
                if (runChild instanceof JAXBElement){
                    JAXBElement element=(JAXBElement)runChild;
                    if (element.getName().getLocalPart().equals ("footnoteReference")){
                        result.add(run);
                    }
                }
            }
        }
        return result;
    }
    public static Object findDefaultRunInParagraph(List<Object> runs){
        int textLength=0;
        Object result=null;
        for (Object obj:runs){
            String rText= TextUtils.getText(obj);
            if (rText.length()>=textLength){
                textLength=rText.length();
                result=obj;
            }
        }
        return result;
    }
    public static  List<MyRun> findMyRunList(String chineseSnippet, List<Object> runs, R defaultRun){
        List<MyRun> result =new ArrayList<MyRun>();
        for (int index=0;index<runs.size();index++){
            if (!RunUtil.equalFormat((R)runs.get(index),defaultRun)){
                if(index-1>=0 && index+1<=runs.size()-1 &&  TextUtils.getText(runs.get(index-1)).endsWith("“") &&   TextUtils.getText(runs.get(index+1)).startsWith("”")){
                    System.out.println("Add MyRun_Type2 "+TextUtils.getText(runs.get(index)));
                    MyRun_Type2 myRun = new MyRun_Type2((R)runs.get(index));
                    result.add(myRun);
                }if (StringUtil.hasMatchStrings("^#\\d+#$",TextUtils.getText(runs.get(index)))){
                    String runText=TextUtils.getText(runs.get(index));
                    System.out.println("Add MyRun_Type3"+runText);
                    MyRun_Type3 myRun=new MyRun_Type3((R)runs.get(index));
                    result.add(myRun);
                }else {
                    String runText=TextUtils.getText(runs.get(index));
                    List<String> matches = StringUtil.getMatchStrings(runText,chineseSnippet);
                    if (matches.size()==1){
                        System.out.println("Add MyRun_Type1"+runText);
                        MyRun_Type1 myRun=new MyRun_Type1((R)runs.get(index));
                        result.add(myRun);
                    }else {
                        System.out.println(runText+"在段落   "+chineseSnippet+"   中出现"+matches.size()+"次，暂时不处理");
                    }
                }
            }
        }

        return result;
    }

    public static boolean equalFormat(R run1, R run2){
        return XmlUtils.marshaltoString(run1.getRPr()).equals(XmlUtils.marshaltoString(run2.getRPr()));
    }

    public static void setRunText(R r,String value){
        for (Object runChild :r.getContent()){
            if (runChild instanceof JAXBElement){
                JAXBElement element=(JAXBElement)runChild;
                if (element.getName().getLocalPart().equals ("t")){
//                    System.out.println( element.getName().getLocalPart());
                    Text text= (Text)element.getValue();
                    text.setValue(value);
                }
            }
        }
    }
    public static void removeRunText(R r){
        int position=-1;
        for (int index=0;index < r.getContent().size();index++){
            if (r.getContent().get(index) instanceof JAXBElement){
                JAXBElement element=(JAXBElement)r.getContent().get(index);
                if (element.getName().getLocalPart().equals("t")){
                    position=index;
                    break;
                }
            }
        }
        if (position !=-1){
            r.getContent().remove(position);
        }
    }
}
